<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="资料名" prop="materialName">
        <el-input
          v-model="queryParams.materialName"
          placeholder="请输入资料名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <div class="wrapper">
      <div v-for="(o, index) in materialList" :key="index">
        <el-card :body-style="{ padding: '0px' }">
          <div class="images">
            <img v-if="checkFileType(o.storagePath)==1" src="@/views/pictures/word-icon.svg" class="image">
            <img v-if="checkFileType(o.storagePath)==2" src="@/views/pictures/excel-icon.svg" class="image">
            <img v-if="checkFileType(o.storagePath)==3" src="@/views/pictures/pdf-icon.svg" class="image">
            <img v-if="checkFileType(o.storagePath)==4" :src="uploadPrefix+o.storagePath" class="image">
            <img v-if="checkFileType(o.storagePath)==5" src="@/views/pictures/video.svg" class="image">
            <img v-if="checkFileType(o.storagePath)=='unknown'" src="@/views/pictures/未知格式.svg" class="image">
          </div>
          <div class="description">
            <span>{{ o.materialName.split('_')[0] }}</span>
            <div class="bottom clearfix">
              <time class="time">{{ o.createTime }}</time>
              <el-button type="text" class="button" @click="fileDownload(o)">点击下载</el-button>
            </div>
          </div>
        </el-card>
      </div>
    </div>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {listMaterial, showMaterial} from "@/api/system/material";

export default {
  name: "Material",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 患教资料管理表格数据
      materialList: [],
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialName: null,
        materialDescription: null,
      },
      uploadPrefix:process.env.VUE_APP_BASE_API,

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询患教资料管理列表 */
    getList() {
      this.loading = true;
      listMaterial(this.queryParams).then(response => {
        this.materialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    //判断文件类型
    checkFileType(path) {
      switch (true) {
        case path.includes('doc')||path.includes('docx'):
          return 1;
        case path.includes('xls')||path.includes('xlsx'):
          return 2;
        case path.includes('pdf'):
          return 3;
        case path.includes('jpg')||path.includes('jpeg')||path.includes('png'):
          return 4;
        case path.includes("mov")||path.includes('mp4'):
          return 5;
        default:
          return "unknown";
      }
    },
    fileDownload(o){
    // 下载
      let a = document.createElement('a');
      let event = new MouseEvent('click');
      a.download = o.materialName;
      a.href = process.env.VUE_APP_BASE_API+o.storagePath;
      a.dispatchEvent(event);
    }
  }
};
</script>

<style scoped>

.wrapper {
  margin: 20px;
  display: grid;
  grid-template-columns: repeat(5, 240px);
  grid-gap: 30px;
  grid-template-rows: 220px;
}
.el-card {
  width: 240px;
  height: 210px;
  cursor: pointer;
}
.el-card:focus,.el-card:hover {
  box-shadow: 0 6px 8px rgba(0, 0, 0, .20), 0 0 6px rgba(0, 0, 0, .04);
}

.images{
  min-height: 110px;
 padding-top: 15px;
}

.image {
  width: 40%;
  height: 40%;
  margin: auto;
  display: block;
}

.description {
  padding: 15px;
  text-align: center;
}

.bottom {
  margin-top: 13px;
  line-height: 12px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both
}

.time {
  font-size: 13px;
  color: #999;
}
.button {
  padding: 0;
  float: right;
}
</style>
